package com.leTest.eduService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leTest.R;
import com.leTest.eduService.entity.TeacherEntity;
import com.leTest.eduService.entity.vo.TeacherQuery;
import com.leTest.eduService.service.TeacherService;
import com.leTest.exceptionhandler.CustomExceptionHandler;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author le
 * @since 2025-02-06
 */
@Api(tags = "教师管理Api")
@RestController
@RequestMapping("/eduService/teacher")
@CrossOrigin
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // http://localhost:8001/eduService/teacher/findAll
    // 查询讲师表所有数据
    // rest风格
    @GetMapping("findAll")
    public R findAllTeacher() {
        List<TeacherEntity> list = teacherService.list();
        return R.ok().data("items", list);
    }

    // 逻辑删除
    @DeleteMapping("{id}")
    public R deleteTeacher(@PathVariable Long id) {
        boolean res = teacherService.removeById(id);
        if (res) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // 分页
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable Long current, @PathVariable Long limit) {
        Page<TeacherEntity> pageT = new Page<>(current, limit);
        teacherService.page(pageT, null);
        Map respMap = new HashMap();
        respMap.put("total", pageT.getTotal());
        respMap.put("rows", pageT.getRecords());
        return R.ok().data(respMap);
    }

    // 条件查询分页
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) TeacherQuery teacherQuery) {
        return R.ok().data(teacherService.getTeacherListByCondition(current, limit, teacherQuery));
    }

    // 添加
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody TeacherEntity teacherEntity) {
        boolean res = teacherService.save(teacherEntity);
        if (res) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    // id查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable Long id) {
        TeacherEntity teacherEntity = teacherService.getById(id);
        return R.ok().data("teacher", teacherEntity);
    }

    // 修改
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody TeacherEntity teacherEntity) {
       boolean res = teacherService.updateById(teacherEntity);
       if (res) {
           return R.ok();
       } else {
           return R.error();
       }
    }
}
